package com.bxd.baodan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.item.PlanerBean;
import com.bxd.baodan.item.PlanerBean.PlanerBean_;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.util.StringUtil;
import com.tencent.TIMConversationType;
import com.tencent.qcloud.timchat.model.Conversation;
import com.tencent.qcloud.timchat.ui.ChatActivity;
import com.tencent.qcloud.timchat.utils.TimeUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * 会话界面adapter
 */
public class PlanerMsgAdapter extends ArrayAdapter<Conversation> {
    private Context context;
    private int resourceId;
    private View view;
    private ViewHolder viewHolder;
    private PlanerBean_ bean_;


    public PlanerMsgAdapter(Context context, int resource, List<Conversation> objects) {
        super(context, resource, objects);
        resourceId = resource;
        this.context = context;
    }

    @Override
    public View getView(int position, final View convertView, ViewGroup parent) {
        if (convertView != null) {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) view.findViewById(R.id.name);
            viewHolder.time = (TextView) view.findViewById(R.id.message_time);
            viewHolder.unread = (TextView) view.findViewById(R.id.unread_num);
            viewHolder.see = (TextView) view.findViewById(R.id.see);
            view.setTag(viewHolder);
        }
        final Conversation data = getItem(position);


        String planerJson = SpUtil.getInstance().getString("planerJson");
        if (!StringUtil.isEmpty(planerJson)) {

            List<PlanerBean_> list = new Gson().fromJson(planerJson, PlanerBean.class).data.list;

            for (int i = 0; i < list.size(); i++) {

                bean_ = list.get(i);

                if ((bean_.id + "").equals(data.getIdentify())) {
                    break;
                }
            }
        }


        String name = bean_.nickname;
        String tv = "保险规划顾问" + name + "给您发来了一条新消息。";
        viewHolder.tvName.setText(tv);

        viewHolder.time.setText(TimeUtil.getTimeStr(data.getLastMessageTime()));

        long unRead = data.getUnreadNum();
        // TODO: 2017/11/16  需要修改对icon的逻辑
        if (unRead <= 0) {


            viewHolder.unread.setBackgroundColor(context.getResources().getColor(R.color.text_gray));
            viewHolder.tvName.setTextColor(context.getResources().getColor(R.color.text_gray));
        } else {
            viewHolder.unread.setBackgroundColor(context.getResources().getColor(R.color.green_lab));
            viewHolder.tvName.setTextColor(context.getResources().getColor(R.color.black));

        }
        viewHolder.see.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String planerJson = SpUtil.getInstance().getString("planerJson");
                if (!StringUtil.isEmpty(planerJson)) {

                    List<PlanerBean_> list = new Gson().fromJson(planerJson, PlanerBean.class).data.list;

                    for (int i = 0; i < list.size(); i++) {

                        bean_ = list.get(i);

                        if ((bean_.id + "").equals(data.getIdentify())) {
                              MobclickAgent.onEvent(App.getContext(), "v6_0_0_WD_xxzx_xx_xq");
                            ChatActivity.navToChat(context, data.getIdentify(), true, bean_, TIMConversationType.C2C);
                        }
                    }
                }








            }
        });


        return view;
    }

    public class ViewHolder {
        public TextView tvName;
        public TextView time;
        public TextView unread;
        public TextView see;


    }
}
